//
//  User.swift
//  GhostWorld
//
//  Created by Kam on 2018/4/3.
//  Copyright © 2018 LKGood. All rights reserved.
//

import Foundation

struct User {
  enum LoginType: Int {
    case system = 0
    case wechat = 1
    case weibo = 2
    case qq = 3
  }
  
  enum Gender: Int {
    case unknown = 0
    case male = 1
    case female = 2
  }
  
  var identifier: Int
  var cellPhone: String?
  var loginType: LoginType
  var nickname: String?
  var avatar: RemoteImage
  var token: String
  var gender: Gender
  var loginDate: Date?
  var hadBeenPremium = false
  var isPremium = false
    var email: String?
  
    init(identifier: Int, cellPhone: String?, loginType: LoginType, nickname: String?, avatar: RemoteImage, token: String, gender: Gender, loginDate: Date?, email : String?) {
    self.identifier  = identifier
    self.cellPhone   = cellPhone
    self.loginType   = loginType
    self.nickname    = nickname
    self.avatar      = avatar
    self.token       = token
    self.gender      = gender
    self.loginDate   = loginDate
    self.email       = email
  }
  
  init(dictionary: [String: Any]) {
    identifier  = dictionary["identifier"] as! Int
    cellPhone   = dictionary["cellPhone"] as? String
    loginType   = LoginType(rawValue: dictionary["loginType"] as! Int)!
    nickname    = dictionary["nickname"] as? String
    avatar      = RemoteImage(dictionary: dictionary["avatar"] as! [String: Any])
    token       = dictionary["token"] as! String
    gender      = Gender(rawValue: dictionary["gender"] as! Int)!
    loginDate   = Date(fromString: dictionary["loginDate"] as! String)
    email       = dictionary["email"] as? String
    if dictionary["hadBeenPremium"] != nil {
      hadBeenPremium = dictionary["hadBeenPremium"] as! Bool
    } else {
      hadBeenPremium = false
    }
    
    if dictionary["isPremium"] != nil {
      isPremium = dictionary["isPremium"] as! Bool
    } else {
      isPremium = false
    }
  }
  
  func getDictionary() -> [String: Any] {
    return [
      "identifier": identifier,
      "cellPhone": cellPhone ?? "",
      "loginType": loginType.rawValue,
      "nickname": nickname ?? "",
      "avatar": avatar.getDictionary(),
      "token": token,
      "gender": gender.rawValue,
      "loginDate": loginDate != nil ? loginDate!.toString() : "",
      "hadBeenPremium": hadBeenPremium,
      "isPremium": isPremium,
      "email": email ?? "",
    ]
  }
}
